package com.telkomsigma.hibernate.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.telkomsigma.hibernate.persistence.player.entity.Player;
import com.telkomsigma.hibernate.persistence.team.entity.Team;
import com.telkomsigma.hibernate.services.player.PlayerService;
import com.telkomsigma.hibernate.services.team.TeamService;


@Controller
@Transactional
public class ControllerPlayer {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	PlayerService playerService = (PlayerService)context.getBean("playerService");
	TeamService teamService = (TeamService)context.getBean("teamService");
	
	
//	@RequestMapping(value="/playerPhoto", method = RequestMethod.GET)
//	public ModelAndView photoForm(@RequestParam("playerId") int playerId){
//		Map<String, Object> teamMap = new HashMap<String, Object>();
//		teamMap.put("modulePlayerMap","photo");
//		teamMap.put("playerId", playerId);
//		return new ModelAndView("player","teamMapAttribute", teamMap);		
//	}
	
	@RequestMapping(value = "/player", method = RequestMethod.GET)
	public ModelAndView listOfTeam(){
		List<Team> teamList = teamService.findAll(0,5);
		Map<String, Object> teamMap = new HashMap<String, Object>();
		teamMap.put("teamListMap", teamList);
		teamMap.put("modulePlayerMap","list");
        return new ModelAndView("player","teamMapAttribute", teamMap);		
	}
		
	@SuppressWarnings("unused")
	@RequestMapping(value = "/player", method = RequestMethod.POST)
	public ModelAndView savePlayer(@RequestParam("teamId") String teamId,
								   @RequestParam("firstName") String firstName,
								   @RequestParam("lastName") String lastName,
								   @RequestParam("salary") double sal_com,
								   @RequestParam("type") String type
								   ){
		Player result;
		
		if (type.equals("player")){ //player
			Player player = new Player();
			player.setFirstName(firstName);
			player.setLastName(lastName);
			player.setTeamId(teamId);
			player.setJoinDate(new Date());
			player.setReserve(0);
			player.setSalary(sal_com);	
			player.setCommision(0D);
			player.setPhotoId(null);			
			result = playerService.insert(player);			
		}else if (type.equals("reserve")){ //reserve
			Player reserve = new Player();
			reserve.setFirstName(firstName);
			reserve.setLastName(lastName);
			reserve.setTeamId(teamId);
			reserve.setJoinDate(new Date());
			reserve.setReserve(1);
			reserve.setSalary(0D);
			reserve.setCommision(sal_com);	
			reserve.setPhotoId(null);
			result = playerService.insert(reserve);
		}	
		String location = "playerManage.htm?teamId=" + teamId;
		Map<String, Object> playerQuery = new HashMap<String, Object>();		
		playerQuery.put("message", "Insert Success");
		playerQuery.put("location", location);
		return new ModelAndView("player","playerQuery", playerQuery);
	}
	
	@RequestMapping(value = "/playerEdit", method = RequestMethod.POST)
	public ModelAndView editPlayer(@RequestParam("teamId") String teamId,
								   @RequestParam("playerId") String playerId,
								   @RequestParam("firstName") String firstName,
								   @RequestParam("lastName") String lastName,
								   @RequestParam("salary") double sal_com,
								   @RequestParam("type") String flagReserve){
		Player result = null;
		
		if (flagReserve.equals("player")){ //player						
			Player player = new Player();
			player.setFirstName(firstName);
			player.setLastName(lastName);
			player.setTeamId(teamId);
			player.setJoinDate(new Date());
			player.setReserve(0);
			player.setSalary(sal_com);					
			player.setPhotoId(null);
			player.setPlayerId(playerId);
			result = playerService.update(player);
		}else if (flagReserve.equals("reserve")){ //reserve			
			Player reserve = new Player();
			reserve.setFirstName(firstName);
			reserve.setLastName(lastName);
			reserve.setTeamId(teamId);
			reserve.setJoinDate(new Date());
			reserve.setReserve(1);
			reserve.setCommision(sal_com);	
			reserve.setPhotoId(null);
			reserve.setPlayerId(playerId);
			result = playerService.update(reserve);
		}	
		String location = "playerManage.htm?teamId=" + teamId;
		Map<String, Object> playerQuery = new HashMap<String, Object>();
		if (result.equals(null)){
			playerQuery.put("message", "Update Fail");
		}else{
			playerQuery.put("message", "Update Success");			
		}
		playerQuery.put("location", location);
		return new ModelAndView("player","playerQuery", playerQuery);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/playerManage", method = RequestMethod.GET)
	public ModelAndView listPlayerManage(@RequestParam("teamId") String teamId){		
		Team teamDetail = teamService.findById(teamId);
		List<Player> playerAll = playerService.findByProperty("teamId", teamId, 0);
		
		Iterator i = playerAll.iterator();
		List<Player> players = new ArrayList<Player>();
		List<Player> reserves = new ArrayList<Player>();
		
		while(i.hasNext()) {
			Player tp = (Player) i.next();			
			if(tp.getReserve()==0){
				players.add(tp);
			}else if(tp.getReserve()==1){
				reserves.add(tp);
			}
		}
				
		Map<String, Object> playerMap = new HashMap<String, Object>();		
		playerMap.put("teamPlayer", teamDetail.getName());
		playerMap.put("teamIdPlayer", teamId);
		playerMap.put("listPlayer", players);		
		playerMap.put("listReserve", reserves);
		playerMap.put("module", "manage");
		return new ModelAndView("player","playerMap",playerMap);
	}
	
	@RequestMapping(value = "/playerForm", method = RequestMethod.GET)
	public ModelAndView setupForm(@RequestParam("teamId") String teamId,@RequestParam("type") String type){
		Team teamName = teamService.findById(teamId);
		
		Map<String, Object> formMap = new HashMap<String, Object>();
		formMap.put("teamId", teamId);
		formMap.put("teamName", teamName.getName());
		formMap.put("module", "add");
		if (type.equals("player")){
			formMap.put("type", "player");
		}else if (type.equals("reserve")){
			formMap.put("type", "reserve");
		}
		return new ModelAndView("player","playerMap", formMap);		
	}
	
	@RequestMapping(value = "/playerEditP", method = RequestMethod.GET)
	public ModelAndView setupFormEdit(@RequestParam("playerId") String playerId){		
		Player playerP = (Player) playerService.findById(playerId);
		Map<String, Object> playerMap = new HashMap<String, Object>();
		playerMap.put("firstName", playerP.getFirstName());
		playerMap.put("lastName", playerP.getLastName());
		playerMap.put("teamId", playerP.getTeamId());
		playerMap.put("reserve", playerP.getReserve());	
		playerMap.put("playerId", playerP.getPlayerId());
		playerMap.put("honor", playerP.getSalary());
		playerMap.put("module", "edit");
		return new ModelAndView("player","playerMap",playerMap);
	}
	
	@RequestMapping(value = "/playerEditR", method = RequestMethod.GET)
	public ModelAndView setupFormEditR(@RequestParam("playerId") String playerId){		
		Player playerP = (Player) playerService.findById(playerId);
		Map<String, Object> playerMap = new HashMap<String, Object>();
		playerMap.put("firstName", playerP.getFirstName());
		playerMap.put("lastName", playerP.getLastName());
		playerMap.put("teamId", playerP.getTeamId());
		playerMap.put("reserve", playerP.getReserve());	
		playerMap.put("playerId", playerP.getPlayerId());
		playerMap.put("honor2", playerP.getCommision());
		playerMap.put("module", "edit");
		return new ModelAndView("player","playerMap",playerMap);
	}
	
	
	@RequestMapping(value = "/playerDelete", method = RequestMethod.GET)
	public ModelAndView deletePlayer(@RequestParam("playerId") String playerId,@RequestParam("teamId") String teamId){
		Player player = new Player();		
		player.setPlayerId(playerId);
		Boolean rowCount = playerService.remove(player);		
		String location = "playerManage.htm?teamId=" + teamId;
		
		Map<String, Object> playerQuery = new HashMap<String, Object>();
		if (rowCount.equals(true)){
			playerQuery.put("message", "Delete Success");
		}else{
			playerQuery.put("message", "Delete Fail");
		}
		playerQuery.put("location", location);
		return new ModelAndView("player","playerQuery", playerQuery);
	}
	
//	@RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
//	public ModelAndView uploadPhoto(@RequestParam("playerId") int playerId, @RequestParam("file") String file){
//		Map<String, Object> playerQuery = new HashMap<String, Object>();
//		playerQuery.put("playerId", playerId);
//		playerQuery.put("link",file);		
//		playerQuery.put("module", "viewPhoto");
//		return new ModelAndView("player","playerMap",playerQuery);
//	}
}
